import Vue from 'vue';
import Vuex from 'vuex';
import {
	http
} from '../common/api/index';

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		cartList: [], // 购物车数据
		userInfo: null, // 用户信息 
		shareCode: null, // 邀请人邀请码
		themeColor: '#F97F33', // 主题色
		themeBgColor: '#F97F33',
		gestureAgain: true, // 是否需要二次验证
		location: {},
		phone: '', //用户登录时多页面共享手机号信息
		partner_id: 0, // 店铺id
		shopInfo: {}, //店铺信息
		cartNum: 0, //购物车角标数量
		isAvoid: '', //是否上架隐藏内容
		devices: [],//搜索到的蓝牙列表
		isConDevice: '0',  //是否有链接的设备
	},
	mutations: {
		setDevices(state, devices) {
		    state.devices = devices;
		},
		setIsConDevice(state, isConDevice) {
		    state.isConDevice = isConDevice;
		},
		getNum(state, num) {
			state.cartNum = num;
		},
		// 获取店铺id
		partner_id(state, partner_id) {
			state.partner_id = partner_id;
		},
		// 店铺信息
		shopInfo(state, info) {
			state.shopInfo = info;
		},
		setBadge(state, num) {
			console.log(state.userInfo.run_type,'state.userInfo.run_type');
			let _index = 3;
			if(state.userInfo.run_type==1){ //入仓 四个导航
				 _index = 3;
			}else{ //非入仓 五个导航
				_index = 3;
			}			
			if (num) {
				if (Number(num) > 99) {
					uni.setTabBarBadge({
						index: _index,
						text: '99+'
					})
				} else {
					uni.setTabBarBadge({
						index: _index,
						text: num + ''
					})
				}
			} else {
				uni.removeTabBarBadge({
					index: _index
				})
			}
		},
		// 上架开关
		shelfAvoid(state, time) {
			state.isAvoid = time;
			global.isAvoid = time;
		},
		// 初始化APP,获取缓存的token,用户信息等其他缓存内容
		initApp(state) {
			const _userInfo = uni.getStorageSync('USER_INFO');
			if (_userInfo) {
				state.userInfo = _userInfo;
				global.userInfo = _userInfo;
				global.token = _userInfo.user_token;
			}
		},
		// 更新用户信息
		updateUserInfo(state, data) {
			if (state.userInfo) {
				state.userInfo = {
					...state.userInfo,
					...data,
				};
			} else {
				state.userInfo = data;
			}
			uni.setStorageSync('USER_INFO', state.userInfo);
			global.userInfo = state.userInfo;
			if (state.userInfo.user_token) {
				global.token = state.userInfo.user_token;
			}
		},
		// 切换主题
		switchThemeColor(state, color) {
			state.themeColor = color;
		},
		// 退出登录
		logout(state) {
			state.userInfo = null;
			// state.cartList = [];
			global.userInfo = null;
			global.token = null;
			// state.verifyAgain = false; // 用户退出时二次验证为false，感觉没必要
			// state.phone = '';
			uni.removeStorageSync('USER_INFO');
		},
		// 二次验证
		setGestureAgain(state, data) {
			state.gestureAgain = data;
		},
		updataLocation(state, data) {
			state.location = data;
		},
		sharePhone(state, data) {
			state.phone = data;
		}
	},
	// 从基本数据(state)派生的数据，相当于state的计算属性
	getters: {
    // 是否为正式版
    isAvoid(state) {
    	return state.isAvoid;
    },
		//购物车修改接口之后仍然使用时因为还有其他页面调用
		allNum(state) {
			return state.cartNum;
		},
		isLogin(state) {
			if (state.userInfo == null) {
				return false;
			} else {
				return true;
			}
		},
		// 是否会员
		isMember(state) {
			if (state.userInfo && state.userInfo.user_level) {
				return true;
			} else {
				return false;
			}
		},
		// 用户ID
		uid(state) {
			if (state.userInfo && state.userInfo.id) {
				return state.userInfo.id;
			} else {
				return 0;
			}
		},
		// 用户Token
		token(state) {
			if (state.userInfo && state.userInfo.user_token) {
				return state.userInfo.user_token;
			} else {
				return null;
			}
		},
		// 用户信息
		userInfo(state) {
			if (state.userInfo) {
				return state.userInfo;
			} else {
				return null;
			}
		},
	},
	// 和mutation的功能大致相同，不同之处在于 ==》1. Action 提交的是 mutation，而不是直接变更状态。 2. Action 可以包含任意异步操作。
	actions: {
		getCartList({
			dispatch,
			commit,
			getters,
			state
		}) {
			if (state.userInfo) {
				http.post('/v1/618a29f69036d').then((res) => {
					console.log('消息角标++++++', res.data.data)
					if (res.data.code == 1) {
						let data = res.data.data.all;
						data = data > 99 ? '99+' : data;
						commit('getNum', data);
						commit('setBadge', data);
					}
				});
			} else {
				commit('setBadge', 0)
			}
		},
		// ios上架规避接口
		getIsShelf({
			dispatch,
			commit,
			getters,
			state
		}) {
			http.post('/v1/61e25fdd5ab27',{
        code:'supplier_up_status'
      }).then((res) => {
				if (res.data.code == 1) {
					let data = res.data.data;
          let type = data == 1 ? true : false;
					//res.data.data   0是上架中，  1是已上架
					commit('shelfAvoid', type);
				}
			});
		}
	},
})
export default store;
